﻿namespace ScrumLite.Services
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Transactions;
    using System.Linq;
    using Contracts;

    public class LinqMeasuresService : IMeasuresService
    {
        ScrumLite.Data.IMeasureStore store;

        public LinqMeasuresService(ScrumLite.Data.IMeasureStore store)
        {
            this.store = store;
        }

        public LinqMeasuresService()
            : this(new ScrumLite.Data.MeasureStore())
        {
        }

        public Measure[] GetMeasuresByProcessName(Guid projectId, string processName)
        {
            List<Measure> query = (from m in this.store.GetMeasures(processName)
                                   from pm in this.store.GetProjectMeasures(projectId, processName)
                                   where m.id == pm.fk_measure
                                   select new Measure 
                                   { 
                                       Name = m.name, 
                                       Unit = m.unit,
                                       LastMeasure = pm.lastMeasure,
                                       Value = pm.value,
                                       LowThresholdValue = m.lowTresholdValue,
                                       HighThresholdValue = m.highTresholdValue,
                                       AscendantMeasure = m.ascendentMeasure
                                   }
                                   ).ToList();

            return query.ToArray();
        }
    }
}
